<?php

namespace app\admin\controller\system;

use think\App;
use app\Request;
use app\common\controller\AdminController;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnnotation;
use app\admin\model\SystemDictionary;

#[ControllerAnnotation(title: '字典管理')]
class Dictionary extends AdminController
{

    private array $notes;

    public function __construct(App $app)
    {
        parent::__construct($app);
        self::$model = new \app\admin\model\SystemDictionary();
        $notes = self::$model::$notes;

        $this->notes = $notes;
        $this->assign(compact('notes'));
    }

    private array $rule = [
        'code'   => 'require',
        'name'   => 'require',
    ];
    private array $message = [
        'code.require'   => '请输入【字典编码】',
        'name.require'   => '请输入【字典名称】',
    ];

    private array $status = [
        ['name' => '启用', 'value' => '1', 'id' => 'yes'],
        ['name' => '禁用', 'value' => '0', 'id' => 'no']
    ];

    #[NodeAnnotation(title: '添加', auth: false)]
    public function add(Request $request): string
    {
        if ($request->isPost()) {
            $post  = $request->post();
            $this->validate($post, $this->rule, $this->message);
            $code = $post['code'];
            $res = $this->dictionaryCodeExist($code);
            if ($res) {
                $this->error("【" . $code . "】" . '字典编码已存在');
            }
            try {
                $post["status"] = 1;
                $save = self::$model::create($post);
            } catch (\Exception $e) {
                $this->error(config("code.operation_error"));
            }
            if ($save) {
                $this->success(config("code.operation_success"));
            } else {
                $this->error(config("code.operation_failed"));
            }
        }
        return $this->fetch();
    }

    #[NodeAnnotation(title: '删除', auth: true)]
    public function delete(Request $request): void
    {
        $this->checkPostRequest();
        $id  = $request->param('id');
        $row = self::$model::whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        $date = $row[0]["'date'"];
        try {
            $save = $row->delete();
        } catch (\Exception $e) {
                $this->error(config("code.operation_error"));
        }

        if ($save) {
            $this->success(config("code.operation_success"));
        } else {
            $this->error(config("code.operation_failed"));
        }
    }

    #[NodeAnnotation(title: '编辑', auth: true)]
    public function edit(Request $request, $id = 0): string
    {
        $row = self::$model::where('id', $id)->find();
        empty($row) && $this->error('数据不存在');
        if ($request->isPost()) {
            $post = $request->post();
            $code = $post['code'];
            if ($row['code'] != $code) {
                $res = $this->dictionaryCodeExist($code);
                if ($res) {
                    $this->error("【" . $code . "】" . '字典编码已存在');
                }
            }
            $this->validate($post, $this->rule, $this->message);
            if (!isset($post['status'])) {
                $this->error("请选择字典状态");
            }
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error(config("code.operation_error"));
            }
            if (!empty($save)) {
                $this->success(config("code.operation_success"));
            } else {
                $this->error(config("code.operation_failed"));
            }
        }
        $this->assign('row', $row);
        $this->assign('status', $this->status);
        return $this->fetch();
    }

    /**
     * 判断字典编码是否存在
     * @access public
     * @param string $date 日期
     */
    public function dictionaryCodeExist(string $code): bool
    {
        $admin = SystemDictionary::where('code', $code)->find();
        if (!empty($admin)) {
            return true;
        } else {
            return false;
        }
    }
}
